//面包屑
// new Vue().$mount('#crumbs');
var msgId = localStorage.getItem("sendedMsgId");
var sm = new Vue({
    el: "#messagesendstatistic",
    data: function () {
        return {
            receiveStatus: '',
            loading: true,
            tableData: [],
            pageIndex: 1,
            pageSize: 10,
            total: 0,
            sels: [],
            // 发送成功，发送失败，全部
            stateCounts: [0, 0, 0],
            curFilterVal: '',
            status: '',
            visible: true
        }
    },
    methods: {
        getNum: function (receiveStatus) {
            VanyAjax("/sendedmsg/getReceiveMsgCount", {
                msgId: msgId,
                receiveStatus: receiveStatus
            }, "POST").done(function (data) {
                if (receiveStatus === 1) {
                    this.stateCounts[0] = data;
                } else if (receiveStatus === 9) {
                    this.stateCounts[1] = data;
                } else {
                    this.stateCounts[2] = data;
                }
                this.stateCounts = [].concat(this.stateCounts);
            }, false, this);
        },
        getData: function (pageIndex, pageSize, msgId, receiveStatus) {
            VanyAjax("/sendedmsg/getReceiveMsgRecord", {
                pageIndex: pageIndex,
                pageSize: pageSize,
                msgId: msgId,
                receiveStatus: receiveStatus
            }, "POST").done(function (data) {
                var msgs = data.list;
                this.tableData = msgs;
                this.total = msgs.length;
                for (var i = 0; i < msgs.length; i++) {
                    this.tableData[i].receiveTime = new Date((msgs[i].receiveTime)).Format("yyyy-MM-dd hh:mm:ss")
                    this.pageIndex = data.pageNum;
                    this.pageSize = data.pageSize;
                    this.total = data.total;
                }
                this.loading = false;
                this.curFilter = "全部(" + msgs.length + ")";
            }, false, this);
        },

        batchSend: function () {
            if (this.sels.length === 0) {
                this.$alert('请选择要发送的消息。', '提示', {
                    type: 'warning',
                    customClass: "notLoadingThis"
                });
                return false;
            }
            var recordIds = this.sels.map(function (row) {
                return row.recordId;
            }).join(",");
            console.log("recordIds:" + recordIds);
            this.sendMessage(recordIds);
        },

        allSend: function () {
            var data = this.tableData;
            var recordIds = data[1].recordId;
            for (var i = 1; i < data.length; i++) {
                recordIds = recordIds + "," + data[i].recordId;
            }
            console.log("recordIds:" + recordIds);
            this.sendMessage(recordIds);
        },

        sendMessage: function (recordIds) {
            var self = this;
            this.$confirm('确认发送所选信息？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                //TODO 调用发送信息接口
            }).catch(function () {
            });
        },

        filterStatus: function (command) {
            this.curFilterVal = command;
            this.getData(1, this.pageSize, msgId, command);
            this.status = command;
        },

        handleCurrentChange: function (val) {
            console.log('当前页:' + val);
            this.getData(val, this.pageSize, msgId, this.receiveStatus);
        },

        handleSizeChange: function (val) {
            this.getData(this.pageIndex, val, msgId, this.receiveStatus);
        },

        handleSelectionChange: function (selection) {
            this.sels = selection;
        }
    },
    computed: {
        curFilter: function () {
            if (this.curFilterVal === "1") {
                return "发送成功(" + this.stateCounts[0] + ")";
            } else if (this.curFilterVal === "9") {
                return "发送失败(" + this.stateCounts[1] + ")";
            } else {
                return "全部(" + this.stateCounts[2] + ")";
            }
        },
        filtersStatusCount: function () {
            return [{
                text: "全部 (" + this.stateCounts[2] + ")",
                value: ""
            }, {
                text: "发送失败 (" + this.stateCounts[1] + ")",
                value: "9"
            }, {
                text: "发送成功 (" + this.stateCounts[0] + ")",
                value: "1"
            }]
        },
        successRate: function () {
            if (this.stateCounts[2] === 0) {
                return 0;
            } else {
                return (this.stateCounts[0] / this.stateCounts[2] * 100).toFixed(1) + '%';
            }
        }
    },
    mounted: function () {
        this.getData(this.pageIndex, this.pageSize, msgId, this.receiveStatus);
        this.getNum(1);
        this.getNum(9);
        this.getNum('');
    }
})
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};